// 这份代码专门处理逻辑层的操作
const userinfoDao = require('../dao/userinfoDao');

const userinfoService = {
    /*
     * 获取个人信息接口
     */
    async getUserInfo(uid) {
        let result;
        try {
            let res = await userinfoDao.getUserInfo(uid);
            if (res.length) {
                result = { success: true, msg: "查询成功", data: res[0] };
            } else {
                result = { success: false, msg: "查询失败" };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询计划失败" };
        }
        return result;
    },
    /*
     * 修改个人信息接口
     */
    async updateUserInfo(uid, userInfo) {
        let { username, college, major, level, bio } = userInfo;
        let result;
        try {
            let res = await userinfoDao.updateUserInfo(uid, username, college, major, level, bio);
            if (res.affectedRows) {
                result = { success: true, msg: "修改成功" };
            } else {
                result = { success: false, msg: "修改失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 获取 专注时长、投递数、收藏数 的接口
     */
    async getHomePageNeedData(uid) {
        let result;
        try {
            let res = await userinfoDao.getHomePageNeedData(uid);
            if (res.length) {
                result = { success: true, msg: "查询成功", data: res[0] };
            } else {
                result = { success: false, msg: "查询失败" };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询计划失败" };
        }
        return result;
    },
    async updateUserIcon(uid, buffer) {
        let result;
        try {
            let res = await userinfoDao.updateUserIcon(uid, buffer);
            if (res.success) {
                result = { success: true, msg: "更新头像成功" };
            } else {
                result = { success: false, msg: "更新头像失败" };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询计划失败" };
        }
        return result;
    }
};

module.exports = userinfoService;
